/*
 * @Author: shanghongyun 2879455630@qq.com
 * @Date: 2025-05-30 16:31:05
 * @LastEditors: shanghongyun 2879455630@qq.com
 * @LastEditTime: 2025-06-19 17:22:53
 * @FilePath: \A05\Drive\qc_protocol.c
 * @Description: QC协议
 */
#include "dev.h"
#include "Base.h"
#include "qc_protocol.h"
#include "charger.h"

qc_protocol_t qc_protocol;

uint8_t gQC_protocol_end = 0;
uint8_t Pre_CVin_is_9 = 0;

#define DELAY_CNT_1 185
#define DELAY_CNT_2 685
#define DELAY_CNT_n 500
#define DELAY_10S_CNT   4000

/**
 * @description: QC诱导9V过程
 * @return {*}
 */
static uint8_t qc_cnt = 0;   /*qc诱导次数*/
#define QC_CNT_MAX          3
void qc_protocol_process(void){
    switch(qc_protocol.state){
        case QC_INIT:
            qc_protocol.delay_cnt++;
            if(qc_protocol.delay_cnt < DELAY_CNT_1){
                QC_LOW;
            }
            else if(qc_protocol.delay_cnt < DELAY_CNT_2){
                QC_HIGH;
            }
            else{
                qc_protocol.delay_cnt = 0;
                qc_protocol.state = QC_LOCK;
            }
            break;


        case QC_NORMAL:
            if(++qc_protocol.delay_cnt > DELAY_CNT_n){
                qc_protocol.delay_cnt = 0;
                qc_protocol.qc_set = !qc_protocol.qc_set;
            }

            if(qc_protocol.qc_set){
                QC_HIGH;
                qc_protocol.qc_set = 0;
                qc_protocol.state = QC_LOCK;
            }
            else{
                QC_LOW;
            }

            break;

        case QC_LOCK:
            if(++qc_protocol.delay_cnt > DELAY_10S_CNT){
                qc_protocol.delay_cnt = 0;
                qc_cnt++;
                qc_protocol.state = QC_END;
            }
            break;

        case QC_END:
            if(qc_cnt < QC_CNT_MAX){
                qc_protocol.state = QC_INIT;
            }
            else{
                qc_cnt = 0;
                Pre_CVin_is_9 = 0;
                gQC_protocol_end = 1;
                qc_protocol.state = QC_SUCCESS;
            }

            if(charger.vin_voltage > 8000){
                qc_cnt = 0;
                gQC_protocol_end = 1;
                Pre_CVin_is_9 = 1;
                qc_protocol.state = QC_SUCCESS;
            }
            break;
            
        case QC_SUCCESS:
            // if(Pre_CVin_is_9 == 1)
            {
                if(charger.vin_voltage < 5500){
                    qc_protocol.state = QC_INIT;
                }
            }
            
            break;
    }
}
